﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace createCatalog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                tbSource.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                tbTarget.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //System.Security.AccessControl.DirectorySecurity dirSec = Directory.GetAccessControl("E:\\System Volume Information");
            string strSource, strTarget;
            strSource = strTarget = "";

            strSource = tbSource.Text.Trim();
            strTarget = tbTarget.Text.Trim();

            if (strSource == "" || strTarget == "")
            {
                MessageBox.Show("请选择路径。");
                return;
            }

            if (strSource.EndsWith(":\\"))
            {
                MessageBox.Show("源路径不能选择根目录");
                return;
            }

            if (strSource == strTarget)
            {
                MessageBox.Show("源目录和目标目录不能相同");
                return;
            }

            if (strTarget.IndexOf(strSource) > -1)
            {
                MessageBox.Show("目标目录不能为原目录的子目录");
                return;
            }


            if (!Directory.Exists(strSource))
            {
                MessageBox.Show("原目录不存在");
                return;
            }

            try
            {
                if (!Directory.Exists(strTarget))
                {
                    if (MessageBox.Show("目标目录不存在，是否创建", "注意", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        return;
                    }

                    Directory.CreateDirectory(strTarget);
                }

                Create(strSource, strTarget);

                MessageBox.Show("目录复制完成");
                tbSource.Text = "";
                tbTarget.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            
        }

        private void Create(string strSrcDir, string strTgtDir)
        {
            string strCurSrcDir = strSrcDir;
            string strCurTgtDir = strTgtDir;
            
            if (strCurSrcDir.EndsWith("\\"))
                strCurSrcDir = strCurSrcDir.Substring(0, strCurSrcDir.Length - 1);
            if (strCurTgtDir.EndsWith("\\"))
                strCurTgtDir = strCurTgtDir.Substring(0, strCurTgtDir.Length - 1);
            
            string strFolderName = "";
            if (strCurSrcDir.LastIndexOf("\\") < 0)
            {
                if (strCurSrcDir.IndexOf(":") < 0)
                {
                    MessageBox.Show("目录路径非法");
                    return;
                }
                else
                {
                    if (strCurSrcDir.EndsWith(":"))
                    {
                        strFolderName = strCurSrcDir.Substring(0, strCurSrcDir.IndexOf(":"));
                    }
                    else
                    {
                        strFolderName = strCurSrcDir.Substring(strCurSrcDir.IndexOf(":") + 1);
                    }
                }
            }
            else
            {
                strFolderName = strCurSrcDir.Substring(strCurSrcDir.LastIndexOf("\\") + 1);
            }

            // d:y\x
            Directory.CreateDirectory(strCurTgtDir + "\\" + strFolderName);
            
            string[] arrDir = null;
            try
            {
                arrDir = Directory.GetDirectories(strCurSrcDir, "*", SearchOption.AllDirectories);
                
            }
            catch (Exception e)
            {
            	
            }
            
            foreach (string strDir in arrDir)
            {
                string aa = strDir.Replace(strCurSrcDir, strCurTgtDir + "\\" + strFolderName);
                Directory.CreateDirectory(aa);
            }
        }
    }
}
